﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Controls = Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Shapes = Windows.UI.Xaml.Shapes;
using Windows.Devices.Input;
using Windows.Foundation;
using BoolanSlide;
using Configuration;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Controls;

namespace BoolanElement
{
    public class BooRoundedRectangle:BooTextShapesBase
    {
        Shapes.Rectangle rect;
        public Shapes.Rectangle Rect 
        {
            get 
            { 
                return rect; 
            }
            set 
            {
                rect = value;
            }
        }

        BooRoundRectElement roundRectElement;
        public BooRoundRectElement RoundRectElement
        {
            get { return roundRectElement; }
            set { roundRectElement = value; }
        }

        public override BooElement Element
        {
            set { roundRectElement = (BooRoundRectElement)value; }
            get { return roundRectElement; }
        }

        public override BooTextShapesBaseElement TextShapesBaseElement
        {
            get { return roundRectElement; }
        }

        public override Shapes.Shape Shape
        {
            get { return rect; }
        }

        public BooRoundedRectangle(BooRoundRectElement element, SaveForRedoUndo saveForRedoUndo)
            : base(element)
        {
            this.roundRectElement = element;
            InitOriginal();           
            Focus.InitFocusEvent(this);
            base.saveForRedoUndo = saveForRedoUndo;
        }

        public BooRoundedRectangle(BooRoundRectElement element)
            : base(element)
        {
            this.roundRectElement = element;
            InitOriginalForThumbNails();
        }

        public override void ReSizeUI(double x, double y)
        {
            if (roundRectElement.ElementTransform.Width + x < 50 || roundRectElement.ElementTransform.Height + y < 50)
            {
                return;
            }

            roundRectElement.ElementTransform.Width = roundRectElement.ElementTransform.Width + x;
            roundRectElement.ElementTransform.Height = roundRectElement.ElementTransform.Height + y;

            roundRectElement.PropertyChanged("Shape");
        }

        public override void ReMargin(double x, double y)
        {
            this.Element.ElementTransform.Translate.X += x;
            this.Element.ElementTransform.Translate.Y += y;
        }

    

        /*protected override void BooUIBase_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            FrameworkElement ell = e.OriginalSource as FrameworkElement;
            if (IsFocusElls(ell))
            {
                return;
            }

            float Rotation = e.Delta.Rotation;
            double xTranslation = e.Delta.Translation.X;
            double yTranslation = e.Delta.Translation.Y;



            if (ShapeMenu != null)
            {
                ShapeMenu.HideMenu();
            }


            Thickness t = Element.ElementTransform.Margin;

            double left = Element.ElementTransform.Margin.Left + xTranslation;
            if (left > -Element.ElementTransform.Width && left < Configuration.UIMargin.MainCanvasRect.Width)
            {
                Element.ElementTransform.Translate.X += xTranslation;
                t.Left += xTranslation;
            }

            double top = Element.ElementTransform.Margin.Top + yTranslation;
            if (top > -Element.ElementTransform.Height && top < Configuration.UIMargin.MainCanvasRect.Height)
            {
                Element.ElementTransform.Translate.Y += yTranslation;
                t.Top += yTranslation;
            }

            Element.ElementTransform.Margin = t;

            e.Handled = true;

        }*/

        public override void InitShape()
        {
            Rect = new Shapes.Rectangle();
            Rect.RadiusX = roundRectElement.RadiusX;
            Rect.RadiusY = roundRectElement.RadiusY;
            Rect.Width = roundRectElement.ShapeWidth;
            Rect.Height = roundRectElement.ShapeHeight;
        }

        bool IsFocusElls(FrameworkElement ell)
        {
            if (ell != null && ell.Tag != null)
            {
                string ellName = ell.Tag.ToString();
                if (ellName == "left_top" || ellName != "middle_top" || ellName != "right_top" || ellName != "right_middle" ||
                ellName != "right_bottom" || ellName != "middle_bottom" || ellName != "left_bottom" || ellName != "left_middle")
                {
                    return true;
                }
            }

            return false;
        }


        public override void ApplySize()
        {
            base.ApplySize();
            roundRectElement.ShapeWidth = roundRectElement.ElementTransform.Width;
            roundRectElement.ShapeHeight = roundRectElement.ElementTransform.Height;

            Rect.Width = roundRectElement.ElementTransform.Width;
            Rect.Height = roundRectElement.ElementTransform.Height;

            FullGrid.Width = roundRectElement.ElementTransform.Width;
            FullGrid.Height = roundRectElement.ElementTransform.Height;

            //BooCanvas.Width = roundRectElement.ElementTransform.Width;
            //BooCanvas.Height = roundRectElement.ElementTransform.Height;

            CenterStack.Width = roundRectElement.ElementTransform.Width;
           // CenterStack.MaxHeight = roundRectElement.ElementTransform.Height;


            this.TextBox.Width = roundRectElement.ElementTransform.Width;
            roundRectElement.TextWidth = roundRectElement.ElementTransform.Width;
           // this.TextBox.MaxHeight = roundRectElement.ElementTransform.Height;
           // roundRectElement.TextMaxHeight = roundRectElement.ElementTransform.Height;


            Focus.FocusElls.ScopeRectangle.Width = roundRectElement.ElementTransform.Width;
            Focus.FocusElls.ScopeRectangle.Height = roundRectElement.ElementTransform.Height;
            Focus.SetFocuseEllipseMargin(roundRectElement.ElementTransform.Width, roundRectElement.ElementTransform.Height);

        }
    }
}
